package com.droid.lib.controls.demo.view;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.droid.lib.controls.demo.R;
import com.droid.lib.controls.util.DragViewUtil;
import com.droid.lib.controls.util.ScreenUtils;
import com.droid.lib.controls.widget.AreaAttribute;
import com.droid.lib.controls.widget.ChinaMapView;
import com.droid.lib.controls.widget.MapRegion;
import com.droid.lib.controls.widget.PathItem;
import com.droid.lib.controls.widget.SvgPathView;
import com.droid.lib.controls.widget.SvgView;
import com.droid.lib.controls.widget.HVScaleScrollView;

import java.util.List;
import java.util.Random;

/**
 * 区域分布图
 */
public class ChinaSvgFragment extends Fragment {
    private HVScaleScrollView msvChina;//地图滑动效果
    private RelativeLayout mrlChina;
    private ChinaMapView mmvChina;//中国地图
    private int mintProjectColor1 = 0xfff5bf12;//颜色1
    private int mintProjectColor2 = 0xffde7a1d;//颜色2
    private int mintProjectColor3 = 0xffc63628;//颜色3
    private int mintSelectdColor = 0xffEB5C47;//点击后的背景色
    float mflScale;
    float mflMapWidth = 1450;
    private SvgView mcsvContent;
    int mintScreenWidth;
    private Button mbtnTab;
    private boolean isbeijing = false;
    private int[] mstrColorArray3 = new int[]{R.color.color_svg_map_bg1, R.color.color_svg_map_bg2, R.color.color_svg_map_bg3, R.color.color_svg_map_bg4};
    private int[] mstrColorArray1 = new int[]{R.color.cyan_500, R.color.teal_500, R.color.green_500, R.color.light_green_500};
    private int[] mstrColorArray2 = new int[]{R.color.unchecked, R.color.colorPrimary, R.color.checked, R.color.jayne_accent};
    private int[] mintCircleRadiuses=new int[34];
    private SvgPathView mspvMap;
    List<PathItem> mListpath;
    public ChinaSvgFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_china_svg, null);
        msvChina = (HVScaleScrollView) view.findViewById(R.id.sv_china);
        mrlChina = (RelativeLayout) view.findViewById(R.id.rl_china);
        mmvChina = (ChinaMapView) view.findViewById(R.id.mv_china);
        mcsvContent = (SvgView) view.findViewById(R.id.csv_content);
        mspvMap = (SvgPathView) view.findViewById(R.id.cpv_path);
        //[{0=北京}],[{1=天津}],[{2=河北}],[{3=山西}],[{4=内蒙古}],[{5=辽宁}],[{6=吉林}],[{7=黑龙江}],[{8=上海}],[{9=江苏}],[{10=浙江}],[{11=安徽}],[{12=福建}],[{13=江西}],[{14=山东}],[{15=河南}],[{16=湖北}],[{17=湖南}],[{18=广东}],[{19=广西}],[{20=海南}],[{21=重庆}],[{22=四川}],[{23=贵州}],[{24=云南}],[{25=西藏}],[{26=陕西}],[{27=甘肃}],[{28=青海}],[{29=宁夏}],[{30=新疆}],[{31=台湾}],[{32=香港}],[{33=澳门}]
        mbtnTab = (Button) view.findViewById(R.id.btn_tab);

        mListpath=mspvMap.getPathItemList();
       PathItem.Location circleLocation= mListpath.get(4).getCircleLocation();
        PathItem.Location textLocation= mListpath.get(4).getTextLocation();
        circleLocation.setX(circleLocation.getX());
        circleLocation.setY((int) (circleLocation.getY()+30f));
        textLocation.setY((int) (textLocation.getY()+30f));
        for(int i=0;i<mListpath.size();i++){
            mListpath.get(i).setMapColor(Color.parseColor(getRandColorCode()));
            final double d = Math.random();
            final int l = (int)(d*70);
            mListpath.get(i).setDataMapRadius(l);
            mListpath.get(i).setMapCircleColor(Color.parseColor("#99F6FCFF"));
        }
        mspvMap.setOnSVGSelectedListener(new SvgPathView.OnSVGSelectedListener() {
            @Override
            public void onSVGSelected(int position, PathItem item, float x, float y) {
                if (mspvMap.getPathItemList().size() == 34) {
                 switch (position){
                     case 0:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.beijing_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 1:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.tianjing_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 2:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.hebei_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 3:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.shanxi_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 4:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.neimenggu_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 5:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.liaoning_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 6:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.jilin_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 7:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.heilongjiang_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 8:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.shanghai_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 9:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.jiangsu_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 10:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.zhejiang_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 11:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.anhui_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 12:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.fujian_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 13:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.jiangxi_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 14:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.shandong_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 15:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.henan_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 16:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.hubei_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 17:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.hunan_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 18:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.guangdong_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 19:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.guanxi_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 20:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.hainan_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 21:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.chongqing_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 22:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.sichuan_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 23:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.guizhou_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 24:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.yunnan_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 25:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.xizang_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 26:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.shaanxi_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 27:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.gansu_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 28:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.qinghai_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 29:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.ningxia_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 30:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.xinjiang_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 31:
//                         mspvMap.initPaint();
//                         mspvMap.initPath(R.raw.taiwan_path);
//                         mspvMap.notifyDataSetChanged();
                         break;
                     case 32:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.xianggang_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                     case 33:
                         mspvMap.initPaint();
                         mspvMap.initPath(R.raw.aomen_path);
                         mspvMap.notifyDataSetChanged();
                         break;
                 }
                    mListpath=mspvMap.getPathItemList();
                    for(int i=0;i<mListpath.size();i++){
                        mListpath.get(i).setMapColor(Color.parseColor(getRandColorCode()));
                        final double d = Math.random();
                        final int l = (int)(d*70);
                        mListpath.get(i).setDataMapRadius(l);
                        mListpath.get(i).setMapCircleColor(Color.parseColor("#99F6FCFF"));
                    }
                }

            }
        });

        mbtnTab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mspvMap.initPaint();
                mspvMap.initPath(R.raw.china_path);
                PathItem.Location circleLocation= mListpath.get(4).getCircleLocation();
                PathItem.Location textLocation= mListpath.get(4).getTextLocation();
                circleLocation.setY((int) (circleLocation.getY()+30f));
                textLocation.setY((int) (textLocation.getY()+30f));
                mListpath=mspvMap.getPathItemList();
                for(int i=0;i<mListpath.size();i++){
                    mListpath.get(i).setMapColor(Color.parseColor(getRandColorCode()));
                    final double d = Math.random();
                    final int l = (int)(d*70);
                    mListpath.get(i).setDataMapRadius(l);
                    mListpath.get(i).setMapCircleColor(Color.parseColor("#99F6FCFF"));

                }
                mspvMap.notifyDataSetChanged();
//                if (isbeijing) {
//                    mcsvContent.setColorArray(mstrColorArray1);
//                    mcsvContent.setNewData(R.raw.jiangsu_svg, (float) 1.5);
//                } else {
//                    mcsvContent.setColorArray(mstrColorArray2);
//                    mcsvContent.setNewData(R.raw.beijing_svg, (float) 0.7);
//                }
//                isbeijing = !isbeijing;
            }
        });
        mcsvContent.setOnProvinceSelectedListener(new SvgView.OnProvinceSelectedListener() {
            @Override
            public void onProvinceSelected(MapRegion region) {
                Toast.makeText(getContext(), "点击了", Toast.LENGTH_SHORT).show();
            }
        });
//        for (int i = 0; i < mmvChina.getMlistAreas().size(); i++) {
//            if (i % 4 == 0) {
//                mmvChina.setPaintColor(i, mstrColorArray1[0], true);
//            }
//            if (i % 4 == 1) {
//                mmvChina.setPaintColor(i, mstrColorArray1[1], true);
//            }
//            if (i % 4 == 2) {
//                mmvChina.setPaintColor(i, mstrColorArray1[2], true);
//            }
//            if (i % 4 == 3) {
//                mmvChina.setPaintColor(i, mstrColorArray1[3], true);
//            }
//        }
        mmvChina.setPaintColor(ChinaMapView.Area.XinJiang, mintProjectColor1, true);
        mmvChina.setPaintColor(ChinaMapView.Area.GanSu, mintProjectColor1, true);
        mmvChina.setPaintColor(ChinaMapView.Area.SiChuan, mintProjectColor1, true);
        mmvChina.setPaintColor(ChinaMapView.Area.GuiZhou, mintProjectColor1, true);
        mmvChina.setPaintColor(ChinaMapView.Area.GuangDong, mintProjectColor1, true);
        mmvChina.setPaintColor(ChinaMapView.Area.AnHui, mintProjectColor2, true);
        mmvChina.setPaintColor(ChinaMapView.Area.AoMen, mintProjectColor2, true);
        mmvChina.setPaintColor(ChinaMapView.Area.ChongQing, mintProjectColor2, true);
        mmvChina.setPaintColor(ChinaMapView.Area.FuJian, mintProjectColor2, true);
        mmvChina.setPaintColor(ChinaMapView.Area.GuangXi, mintProjectColor2, true);

        mmvChina.setPaintColor(ChinaMapView.Area.HaiNan, mintProjectColor3, true);
        mmvChina.setPaintColor(ChinaMapView.Area.HeBei, mintProjectColor3, true);
        mmvChina.setPaintColor(ChinaMapView.Area.HeiLongJiang, mintProjectColor3, true);
        mmvChina.setPaintColor(ChinaMapView.Area.HeNan, mintProjectColor3, true);
        mmvChina.setPaintColor(ChinaMapView.Area.JiangSu, mintProjectColor3, true);
        mmvChina.setSelectdColor(mintSelectdColor);
        mmvChina.setOnProvinceDoubleClickListener(new ChinaMapView.OnProvinceDoubleClickListener() {
            @Override
            public void onProvinceDoubleClick() {
                scaleAndScroll();
            }
        });
        mmvChina.setMapColor(0xffefab14);
        mmvChina.setOnProvinceSelectedListener(new ChinaMapView.OnProvinceSelectedListener() {
            @Override
            public void onProvinceSelected(ChinaMapView.Area pArea, boolean repeatClick) {
                //                Log.e(TAG, "onProvinceSelected: " + pArea.name + "  " + pArea.value);
                if (pArea.value == 0) {
                    mcsvContent.setColorArray(mstrColorArray2);
                    mcsvContent.setNewData(R.raw.beijing_svg, (float) 0.7);
                }
                if (pArea.value == 9) {
                    mcsvContent.setColorArray(mstrColorArray1);
                    mcsvContent.setNewData(R.raw.jiangsu_svg, (float) 1.5);
                }
                if (pArea.value == 28) {
                    mcsvContent.setColorArray(mstrColorArray3);
                    mcsvContent.setNewData(R.raw.xinjiang_svg, (float) 0.7);
                }
                if (repeatClick) {
                    mmvChina.selectAProvince(pArea);
                    return;
                }
            }


//            public void onProvinceSelected(AreaAttribute pArea, boolean repeatClick) {
////                Log.e(TAG, "onProvinceSelected: " + pArea.name + "  " + pArea.value);
//                if (pArea.getNom() == 0) {
//                    mcsvContent.setColorArray(mstrColorArray2);
//                    mcsvContent.setNewData(R.raw.beijing_svg, 1);
//                }
//                if (pArea.getNom() == 9) {
//                    mcsvContent.setColorArray(mstrColorArray1);
//                    mcsvContent.setNewData(R.raw.jiangsu_svg, (float) 2);
//                }
//                if (pArea.getNom() == 28) {
//                    mcsvContent.setColorArray(mstrColorArray3);
//                    mcsvContent.setNewData(R.raw.xinjiang_svg, (float) 2);
//                }
//                if (repeatClick) {
//                    scaleAndScroll();
//                    return;
//                }
//
////                tvData.setText("名称：" + pArea.name + "   值：" + pArea.value);
//            }

        });
        for(int i=0;i<34;i++){
            final double d = Math.random();
            final int l = (int)(d*70);
            mintCircleRadiuses[i]=l;
        }
        mmvChina.setCircleRadiuses(mintCircleRadiuses);
        mintScreenWidth = ScreenUtils.getScreenWidth(getContext());
        mflScale = mintScreenWidth / mflMapWidth;


        msvChina.postDelayed(new Runnable() {
            @Override
            public void run() {
                scaleAndScroll();

            }
        }, 800);
        msvChina.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

            }
        });
        return view;
    }

    private void scaleAndScroll() {
        mrlChina.setScaleX(mflScale);
        mrlChina.setScaleY(mflScale);
        msvChina.smoothScrollTo((mrlChina.getWidth() - mintScreenWidth) / 2, (int) (mrlChina.getHeight() - (mflScale * mflMapWidth)) / 2 + 200);
    }
    /**
     * 获取十六进制的颜色代码.例如 "#6E36B4" , For HTML ,
     *
     * @return String
     */
    public static String getRandColorCode() {
        String r, g, b;
        Random random = new Random();
        r = Integer.toHexString(random.nextInt(256)).toUpperCase();
        g = Integer.toHexString(random.nextInt(256)).toUpperCase();
        b = Integer.toHexString(random.nextInt(256)).toUpperCase();
        r = r.length() == 1 ? "0" + r : r;
        g = g.length() == 1 ? "0" + g : g;
        b = b.length() == 1 ? "0" + b : b;
//        LogUtils.e("");
        return "#" + r + g + b;
    }


}